package by.dkusch.aircompany.webapp.page.base.common;

import org.apache.wicket.Page;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;

import by.dkusch.aircompany.model.tables.base.BaseEntity;
import by.dkusch.aircompany.webapp.page.base.BaseDetailLayout;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.utils.GrantsResolver;

public class AddNewItemPanel<ID, Entity extends BaseEntity<ID>, P extends BaseLayout> extends Panel {

	private static final long serialVersionUID = 1L;

	public AddNewItemPanel(String id, final Entity entity, final Class<P> page) {
		super(id);
		add(new Link<Void>("new") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new BaseDetailLayout<ID, Entity>(entity, ActionType.EDIT, new PageCreator() {

					private static final long serialVersionUID = 1L;

					@Override
					public Page createPage() {
						try {
							return page.newInstance();
						} catch (InstantiationException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						}
						return null;
					}
				}));

			}
		});
		setVisible(GrantsResolver.canDo(entity.getClass(), ActionType.NEW));


	};
}
